# http://210.45.176.205:6000/sjwg/student.jsp
import urllib.request
from bs4 import BeautifulSoup
import urllib.error
import smtplib
from email.mime.text import MIMEText
import time
import smtplib

var = input("输入 楼号-宿舍号 示例: xx-xxx\n")
# var = '10-311'

def getUrl(arg):
    # 请求的链接
    url = "http://210.45.176.205:6000/sjwg/menu/getStudent.action?dex={num}".format(
        num=arg)
    return url

# 获取并解析请求得到的回复
def getres():
    # 请求的头信息
    head = {}
    head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134'
    # 请求所带的数据:
    data = {}
    data = urllib.parse.urlencode(data).encode('utf-8')

    # 生成请求
    req = urllib.request.Request(getUrl(var), data, head)
    try:
        response = urllib.request.urlopen(req)
    except urllib.error.URLError as e:
        print(e.reason)
    else:
        #     # 对回复读取并解码
        html = response.read().decode('utf-8')
        # 通过BeautifulSoup来解析html
        soup = str(BeautifulSoup(html, "html.parser"))
        res = ''
        for i in soup:
            if i == '1' or i == '2' or i == '3' or i == '4' or i == '5' or i == '6' or i == '7' or i == '8' or i == '9' or i == '0' or i == '-'or i == '.'or i == '=' or i == ':':
                # res.append(i)
                if i == "=":
                    i = " "
                res += '%s' % i
        result = ''
        for j in res[2:]:
            result += j
        res = result.split(' ')
        balance = res[0]
        Recharge = res[1]
        times = res[2]
        readout = res[3]
        # time.sleep(60)
        return res
if __name__ == '__main__':
    while 1:
        res= getres()
        balance = res[0]
        msg = MIMEText('当前余额%s'%balance, 'plain', 'utf-8')
        # print(msg)

        # 输入Email地址和口令:
        from_addr = ''
        password = ''#这里不是密码 是授权码 从邮箱设置里面获取
        # 输入收件人地址:
        to_addr = '1031346629@qq.com'
        # 输入SMTP服务器地址:
        smtp_server = 'smtp.qq.com'

        server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
        server.set_debuglevel(1)
        server.login(from_addr, password)
        server.sendmail(from_addr, [to_addr], msg.as_string())
        server.quit()

        time.sleep(6)
